package com.example.zqm.git72.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zqm.git72.JavaBean.Club;
import com.example.zqm.git72.Model.ClubModel;
import com.example.zqm.git72.R;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ClubDetailActivity extends AppCompatActivity {
    String id;
    private TextView intro;
    private TextView time;
    private TextView place;
    private TextView master_name;
    private ImageView clubpic;
    private TextView tel;
    private TextView name;
    public Club club;
    public TextView course;
    public TextView video;
    private ImageButton collect;
    String user_id;
    String club_id;
    String fan_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clubdetail);
        intNN();
        initView();
        initTT();
        iscollecting();

    }
    public void initView(){
        intro = (TextView)findViewById(R.id.intro);
        name = (TextView)findViewById(R.id.name);
        place =(TextView)findViewById(R.id.place);
        time =(TextView)findViewById(R.id.time);
        master_name=(TextView)findViewById(R.id.master_name);
        tel=(TextView)findViewById(R.id.tel);
        clubpic=(ImageView)findViewById(R.id.clubpic);
        video=(TextView)findViewById(R.id.video);
        course=(TextView)findViewById(R.id.course);
        collect=(ImageButton)findViewById(R.id.collect);
        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ifcollecting();
            }
        });
    }
    public void initTT(){
        course.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ClubDetailActivity.this,ClubCourseActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
       video.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent();
               intent.setClass(ClubDetailActivity.this,ClubVideoActivity.class);
               intent.putExtra("id",id);
               startActivity(intent);
           }
       });


    }
    protected void intNN(){
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        id=bundle.getString("id");
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id=sharedPreferences.getString("user_id","");
        ClubModel clubModel =new ClubModel();
        Call<Club> call = clubModel.getClubById(id);
        call.enqueue(new Callback<Club>() {
            @Override
            public void onResponse(Call<Club> call, Response<Club> response) {
                club = response.body();
                name.setText(club.getClub_name());
                intro.setText(club.getIntroduction());
                place.setText(club.getProvince_name()+club.getCity_name()+club.getDistrict_name());
                tel.setText(club.getClub_tel());
                time.setText(club.getOpen_time());
                master_name.setText(club.getHits());
                String picUrl = "http://172.24.10.175/workout/Uploads/" + club.getThumb();
                Picasso.with(ClubDetailActivity.this).load(picUrl.toString())
                        .resize(200,50).into(clubpic);
            }

            @Override
            public void onFailure(Call<Club> call, Throwable t) {

            }
        });

    }

    public void ifcollecting(){
        fan_id=id;
        ClubModel clubModel= new ClubModel();
        Call<Integer> call =clubModel.ifcollect(fan_id,user_id);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body().toString().equals("1")) {
                    collect.setImageResource(R.drawable.shoucang);
                    iscollecting();
                }
                else if(response.body().toString().trim().equals("0")){
                    collect.setImageResource(R.drawable.shouchang1);
                decollecting();
            }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });

    }

    public  void  iscollecting(){
     collect.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             fan_id = id;
             ClubModel clubModel =new ClubModel();
             Call<Integer> call =clubModel.iscollect(fan_id,user_id);
             call.enqueue(new Callback<Integer>() {
                 @Override
                 public void onResponse(Call<Integer> call, Response<Integer> response) {
                     Toast.makeText(ClubDetailActivity.this,"关注成功",Toast.LENGTH_SHORT).show();
                     collect.setImageResource(R.drawable.shoucang);

                 }
                 @Override
                 public void onFailure(Call<Integer> call, Throwable t) {
                     Toast.makeText(ClubDetailActivity.this,"关注失败",Toast.LENGTH_SHORT).show();
                 }
             });
         }
     });
    }
    public  void  decollecting()
    {
        fan_id = id;
        ClubModel clubModel =new ClubModel();
        Call<Integer> call =clubModel.decollect(fan_id,user_id);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                        Toast.makeText(ClubDetailActivity.this,"取消关注成功",Toast.LENGTH_SHORT).show();
                       collect.setImageResource(R.drawable.shouchang1);


            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
}
}
